<!--

/*
#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################
*/

function rememberTab(recHash) {
	cArr = getCookieList('remember');
	nArr = new Array();
	for (i=0; i<cArr.length; i++) {
		split = cArr[i].split('|');
		
		if (recHash == split[1]) {
			split[2] = 'r';
		}
		nArr.push( split.join('|') );
	}
	createCookie( 'remember', nArr.join(':') );
}

function forgetTab(recHash) {
	cArr = getCookieList('remember');
	nArr = new Array();
	for (i=0; i<cArr.length; i++) {
		split = cArr[i].split('|');
		
		if (recHash == split[1]) {
			split[2] = 'f';
		}
		nArr.push( split.join('|') );
	}
	createCookie( 'remember', nArr.join(':') );
}

function markTabActive(tabNum) {
	cArr = getCookieList('remember');
	nArr = new Array();
	for (i=0; i<cArr.length; i++) {
		split = cArr[i].split('|');
		
		if (tabNum == split[0]) {
			split[3] = 'y';
		}
		else {
			split[3] = 'n';
		}
		nArr.push( split.join('|') );
	}
	createCookie( 'remember', nArr.join(':') );
}

function getFirstAvailable() {
	/* format:  tab|hash|remembered|active */
	cArr = getCookieList('remember');
	found = 0;
	/* 1-7 for the number of tabs */
	for (t=1; t<=7; t++) {
		found = 0;
		/* check existing tab data to see if one is open*/
		for (i=0; i<cArr.length; i++) {
			split = cArr[i].split('|');
			if (t == split[0]) {
				if (split[2] != 'r') {
					return t;
				}
				/* this tab is still in use */
				found = 1;
				break;
			}
		}
		if (found != 1) {
			return t;
		}
	}
	return -1;
}

function addTab(recHash) {
	cArr = getCookieList('remember');
	/* check for duplicates, if duplicate found, just bring that tab to the front */
	for (i=0; i < cArr.length; i++) {
		split = cArr[i].split('|');
		if (split[1] == recHash) {
			markTabActive(split[0]);
			return;
		}
	}
	/* no dups, check for available tab */
	tab = getFirstAvailable();
	if (tab < 0) {
		alert('All tabs are in use. Open a tab and \'Forget\' it to make room.');
		return;
	}
	/* index of tab, -1 if not found */
	index = -1;
	/* find tab if exists */
	for (i=0; i < cArr.length; i++) {
		split = cArr[i].split('|');
		if (split[0] == tab) {
			index = i;
		}
	}
	/* no tab exists, add it */
	entry = tab + '|' + recHash + '|r|y';
	if (index < 0) {
		cArr.push(entry);
	}
	else {
		cArr[index] = entry;
	}
	createCookie('remember',cArr.join(':'));
	markTabActive(tab);
	tabFlash('rtab'+tab);
}

function anyTabRemembered() {
	cArr = getCookieList('remember');
	for (i=0; i < cArr.length; i++) {
		split = cArr[i].split('|');
		if (split.length > 2) {
			if (split[2] == 'r') {
				return 1;
			}
		}
	}
	return 0;
}

function forgetAll() {
	cArr = getCookieList('remember');
	nArr = new Array();
	for (i=0; i<cArr.length; i++) {
		split = cArr[i].split('|');
		if (split.length > 2) { 
			split[2] = 'f';
			nArr.push( split.join('|') );
		}
	}
	createCookie( 'remember', nArr.join(':') );

}

function initialTabs() {
	if (anyTabRemembered() == 1) {
		slidePeek();
	}
}


/* have the background of a tab flash a color for a time */
function tabFlash(tabId,onFlash,color,seconds) {
	/* defaults */
	if (onFlash == null) {
		onFlash = 1;
	}
	color = color || '#CCFDFD';
	seconds = seconds || 6;
	/* flash */
	tab = document.getElementById(tabId);
	if (onFlash == 1) {
		tab.style.background = color;
	}
	else {
		tab.style.background = '#FFFFF0';
	}
	seconds -= 1;
	onFlash = (onFlash + 1) % 2;
	if (seconds > 0) {
		call = 'tabFlash(\''+tabId+'\','+onFlash+',\''+color+'\','+seconds+')';
		window.setTimeout(call,750);
	}
}

/* add border to rememberwin */
function addBorder() {
	rw = document.getElementById('rememberwin');
	rw.style.width = rw.offsetWidth - 2;
	rw.style.height = rw.offsetHeight - 2;
	rw.style.borderWidth = 2;
}

/* 0=hidden | 1=showing | 2=peeking */
rw_visible = 0;

/* 0=standing | 1=moving to peek | 2=moving to visible | 3=moving to hidden */
rw_moving = 0;

/* check to see where the window should be after clicking remember*/
function checkMove(shrunk) {
	if (rw_moving == 0) {
		if (shrunk == 0) {
			if (rw_visible == 0) {
				slidePeek();
			}
		}
		else {
			slideOn();
		}
	}
}

/* instant showing of rememberwin */
function showwin(fadein) {
	rw = document.getElementById('remembermaster');
	rw.style.left = "5px";
	if (rw_visible == 0) {
		rw.style.visibility = 'visible';
		/* rw.style.opacity = 0.95; */
		rw_visible = 1;
	}
}

/* instant hiding of rememberwin */
function closewin() {
	document.getElementById('remembermaster').style.left = "-2000px";
	document.getElementById('remembermaster').style.visibility = 'hidden';
	rw_visible = 0;
}

/* replace the hide/show arrow */
function swapArrow() {
	rwsh = document.getElementById('rwsh');
	if (rw_visible == 2) {
		rwsh.innerHTML = '<a onclick="slideOn()" title="Show">&#9658;</a>';
	}
	else {
		rwsh.innerHTML = '<a onclick="slidePeek()" title="Hide">&#9668;</a>';
	}
}

/* slide the window to where only the tabs are showing */
function slidePeek() {
	if (rw_moving != 1 && rw_moving != 0) { return; }
	rw_moving = 1;
	rwm = document.getElementById('remembermaster');
	targ = - document.getElementById('rememberwin').offsetWidth;
	curr = rwm.offsetLeft;
	dist = Math.abs(curr - targ);
	/* calculate speed (with mininum) */
	speed = parseInt(Math.max( 12, Math.abs(dist/8) ));
	if (curr > targ) {
		speed = -speed;
	}
	/* move window */
	if (dist > 10) {
		rwm.style.left = (curr + speed) + "px";
		/* rwm.style.right = curr - (dist / 2); */
		window.setTimeout('slidePeek()',100);
	}
	else {
		rwm.style.left = targ + "px";
		rw_visible = 2;
		rw_moving = 0;
		swapArrow();
	}
}

/* slide the window to where none of it is visible (offscreen) */
function slideOff() {
}

/* slide the window to where all of it is showing (onscreen) */
function slideOn() {
	if (rw_moving != 2 && rw_moving != 0) { return; }
	rw_moving = 2;
	rwm = document.getElementById('remembermaster');
	targ = 4;
	curr = rwm.offsetLeft;
	dist = Math.abs(curr - targ);
	/* calculate speed (with mininum) */
	speed = parseInt(Math.max( 12, Math.abs(dist/8) ));
	if (curr > targ) {
		speed = -speed;
	}
	/* move window */
	if (dist > 10) {
		rwm.style.left = (curr + speed) + "px";
		/* rwm.style.right = curr - (dist / 2); */
		window.setTimeout('slideOn()',100);
	}
	else {
		rwm.style.left = targ + "px";
		rw_visible = 1;
		rw_moving = 0;
		swapArrow();
	}
}

//-->

